package com.memory.wechat.controller;

import com.google.common.collect.Lists;
import com.memory.common.utils.ResultVoUtil;
import com.memory.common.vo.ResultVo;
import com.memory.modules.business.domain.earn.AccountEarn;
import com.memory.modules.business.domain.sell.InvoiceRecord;
import com.memory.modules.business.domain.sell.Sell;
import com.memory.modules.business.domain.user.Account;
import com.memory.modules.business.service.earn.AccountEarnRecordService;
import com.memory.modules.business.service.earn.AccountEarnService;
import com.memory.modules.business.service.earn.CurDayPriceService;
import com.memory.modules.business.service.sell.InvoiceRecordService;
import com.memory.modules.business.service.sell.SellService;
import com.memory.modules.business.service.user.AccountService;
import com.memory.wechat.controller.validator.convert.BuyConvert;
import com.memory.wechat.controller.validator.param.InvoiceApplyParam;
import com.memory.wechat.controller.validator.param.WithdrawParam;
import com.memory.wechat.controller.validator.result.BuyRecordVo;
import com.memory.wechat.controller.validator.result.StatisticsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @PackageName: com.memory.wechat.controller
 * @ClassName: BuyController
 * @Description: 购买记录
 * @author: zhuguangxun
 * @date 2021/3/16
 */
@Controller
@RequestMapping("account")
@RequiredArgsConstructor
public class AccountConterController {

    private final AccountService accountService;

    private final AccountEarnService accountEarnService;

    private final InvoiceRecordService invoiceRecordService;

    private final CurDayPriceService curDayPriceService;

    private final SellService sellService;


    @GetMapping("/earn/list")
    public String list(@RequestParam("openid") String openid, Model model) {
        final List<BuyRecordVo> record = Optional.ofNullable(accountService.findByOpenId(openid))
                .map(e -> Optional.ofNullable(curDayPriceService.findByAccountId(e.getId())).orElse(Lists.newArrayList())
                        .stream().map(BuyConvert::convert).collect(Collectors.toList()))
                .orElse(Lists.newArrayList());

        final StatisticsVo statisticsVo = Optional.ofNullable(accountService.findByOpenId(openid))
                .map(e -> accountEarnService.findByAccountId(e.getId())
                        .orElse(new AccountEarn()))
                .map(e -> StatisticsVo.builder().accountId(e.getAccountId()).historyEarn(e.getEarnPrice()).allowWithdraw(e.getWithdrawPrice()).build())
                .map(e -> e.setTotalCount(sellService.findByAccountId(e.getAccountId()).stream().map(Sell::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add)))
                .orElseGet(null);
        model.addAttribute("statistics", statisticsVo);
        model.addAttribute("records", record);
        model.addAttribute("openid", openid);
        return "/personal/earnrecords";
    }

    @GetMapping("withdraw")
    public String withdrawPage(String openid, String recordId, Model model) {
        model.addAttribute("openid", openid);
        model.addAttribute("recordid", recordId);
        return "/personal/withdrawapply";
    }

    @PostMapping("apply/withdraw")
    public ResultVo withdraw(@RequestBody WithdrawParam param) {
        return ResultVoUtil.success();
    }

    @PostMapping("apply/invoice")
    public ResultVo<Boolean> applyInvoice(@RequestBody InvoiceApplyParam param) {
        final Account account = accountService.findByOpenId(param.getOpenId());
        InvoiceRecord record = new InvoiceRecord();
        record.setAddress(param.getAddress());
        record.setInvoiceNo(param.getInvoiceNum());
        record.setEmail(param.getEmail());
        record.setTitle(param.getTitle());
        record.setPostCode(param.getPostCode());
        record.setAccountId(account.getId());
        record.setCreateDate(new Date());
        final InvoiceRecord invoiceRecord = invoiceRecordService.create(record);
        return ResultVoUtil.success(invoiceRecord != null);
    }


}
